﻿/////////////////////////////////////////////////////////////////
//File: BlogManager.cs
//Description: 此文件是根据facade模式和接口隔离原则创建的。各个子业务之间的关联在这里建立。 
//            
//Copyright© 2011 Moliu
//History:
//  2011-3-26 18:11:20    CodeSmith5.0.1    Original Version
//  2011-3-26 19:26:20    Moliu             1
//  2011-4-17 13:13:15    Moliu             2
//  2011-4-23 12:21:49    Moliu             3
//  2011-4-22 22:10:12    Moliu             4
/////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Security.Permissions;
using System.Transactions;

using MAF.Common;
using MAF.Common.Security;
using My.Blog.Domain.Entities;
using My.Blog.Domain.Interfaces;
using My.Blog.Business.Persistence;
using My.Blog.Business.Security;
using Services.Business.Service;
using MAF.CRUD;
using My.Blog.Database;

namespace My.Blog.Business.Service
{
    public partial class CategorySvc : BusinessCRUD<CategoryInfo>,ICRUD<CategoryInfo>
    {
        #region 构造函数
        public CategorySvc(string module) : base(StorageFactory.GetInstance(module).CreateCategory()){}
        #endregion

        #region ICategory 成员
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public override void  Add(CategoryInfo info )
        {
            if (string.IsNullOrEmpty(info.Title))
                throw new ArgumentException("请填写分类标题");

            info.DateCreated = DateTime.Now;
            return _Category.CreateCategory(info);
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public override void Update(CategoryInfo info )
        {
            if (string.IsNullOrEmpty(info.Title))
                throw new ArgumentException("请填写分类标题");

            info.DateModified = DateTime.Now;
            return _Category.UpdateCategory(info);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public override int Delete(CategoryInfo info )
        {
            //ICategory _Category = DaoFactory.Instance.CreateCategory();

            string[] ss = info.Ids.Split(", ".ToCharArray());
            int r = 0;
            for (int i = 0, len = ss.Length; i < len; i++)
            {
                if( string.IsNullOrEmpty(ss[i]) )
                    continue;

                info.Id = int.Parse(ss[i]);
                r += _Category.DeleteCategories(info);
            }
            return r;
        }
        
        
        #endregion

        //================================
        // 上面是自动生成的代码,下面是手工编写的代码
        //================================ 

        #region ICategory 成员

        /// <summary>
        /// 树顺序排列的Categories列表
        /// </summary>
        public IList<CategoryInfo> ReadCategoriesByConditions(CategoryInfo info)
        {
            //ICategory _Category = DaoFactory.Instance.CreateCategory();
            return _Category.ReadCategoriesByConditions(info);
        }

        /// <summary>
        /// 祖先节点
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public IList<CategoryInfo> ReadAncestorsByConditions(CategoryInfo info)
        {
            //ICategory _Category = DaoFactory.Instance.CreateCategory();
            return _Category.ReadAncestorsByConditions(info);
        }

        /// <summary>
        /// 移动节点
        /// </summary>
        /// <param name="sourceId"></param>
        /// <param name="targetId"></param>
        /// <param name="width"></param>
        /// <returns></returns>
        public int MoveCategoryTo(int sourceId, int targetId, int width)
        {
            //ICategory _Category = DaoFactory.Instance.CreateCategory();
            return _Category.MoveCategoryTo(sourceId, targetId, width);
        }

        #endregion
    }
}
